package service;

import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.WakefulBroadcastReceiver;
import android.util.Log;
import app.cloudstringers.Home;
import app.cloudstringers.R;

import com.google.android.gms.gcm.GoogleCloudMessaging;

import define.Key;

public class GcmIntentService extends IntentService {
	/**
	 * String section
	 */
   // public static final int NOTIFICATION_ID = 1;

    private String TAG = GcmIntentService.class.getSimpleName();
    
    /**
     * The others section
     */
   // private NotificationManager mNotificationManager;
//    private NotificationCompat.Builder builder;
	
    public GcmIntentService() {
        super("GcmIntentService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        Bundle extras = intent.getExtras();
        GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
        // The getMessageType() intent parameter must be the intent you received
        // in your BroadcastReceiver.
        String messageType = gcm.getMessageType(intent);

        if (!extras.isEmpty()) {  
        	// has effect of unparcelling Bundle
            /*
             * Filter messages based on message type. Since it is likely that GCM
             * will be extended in the future with new message types, just ignore
             * any message types you're not interested in, or that you don't
             * recognize.
             */
            if (GoogleCloudMessaging.
                    MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
                sendNotification("Send error: " + extras.toString());
            } else if (GoogleCloudMessaging.
                    MESSAGE_TYPE_DELETED.equals(messageType)) {
                sendNotification("Deleted messages on server: " +
                        extras.toString());
            // If it's a regular GCM message, do some work.
            } else if (GoogleCloudMessaging.
                    MESSAGE_TYPE_MESSAGE.equals(messageType)) {

            	// TODO
            	define.Home.NOTIFICATION = 
            			extras.get(Key.KEY_RECEIVE_GCM_NOTIFICATION).toString();
            	
            	Log.i(TAG, "" + define.Home.NOTIFICATION);
            	
                // Post notification of received message.
                sendNotification(define.Home.NOTIFICATION);
               
            }
        }
        
        // Release the wake lock provided by the WakefulBroadcastReceiver.
        WakefulBroadcastReceiver.completeWakefulIntent(intent);
    }

    // Put the message into a notification and post it.
    // This is just one simple example of what you might choose to do with
    // a GCM message.
    private void sendNotification(String msg) {
    	Notification.InboxStyle inboxstyle = new Notification.InboxStyle();
    	String [] events = new String[1];
    	events[0] = new String (msg);
    	for(int i=0; i<events.length;i++ ){
    		inboxstyle.addLine(events[i]);
    	}
    	
    	NotificationManager notifimng = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
    	Intent intent = new Intent (this, Home.class);
    	PendingIntent pintent = PendingIntent.getActivity(this, 0, intent, 0);
    	Notification notifi = new Notification.Builder(this)
    		.setContentTitle("Notification")
    		.setSmallIcon(R.drawable.ic_launcher)
    		.setContentIntent(pintent)
    		.setStyle(inboxstyle)
    		//.setStyle(new Notification.BigTextStyle().bigText(msg))
    		.build();
    	notifimng.notify(0, notifi);
    	
      /* mNotificationManager = (NotificationManager)
              this.getSystemService(Context.NOTIFICATION_SERVICE);

        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
               new Intent(this, Home.class), 0);
        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.ic_launcher).setContentTitle("");
        
        // Set Big View style to see full content of the message
        NotificationCompat.BigTextStyle inboxStyle =
               new NotificationCompat.BigTextStyle();
        
        inboxStyle.setBuilder(mBuilder);
        inboxStyle.bigText("");
        inboxStyle.setBigContentTitle("");
        inboxStyle.setSummaryText("");
        // Moves the big view style object into the notification object.
        mBuilder.setStyle(inboxStyle);
        mBuilder.setContentText(msg);
        mBuilder.setDefaults(Notification.DEFAULT_ALL);
        mBuilder.setContentIntent(contentIntent);
        mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build()); */
    	
    	/*Intent intent = new Intent(this, Home.class);
    	PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);

    	// build notification
    	// the addAction re-use the same intent to keep the example short
    	Notification n  = new Notification.Builder(this)
    	        .setContentTitle("New mail from " + "test@gmail.com")
    	        .setContentText(msg)
    	        .setSmallIcon(R.drawable.ic_launcher)
    	        .setContentIntent(pIntent)
    	        .setStyle(new Notification.BigTextStyle().bigText(msg))
    	        .build();
    	NotificationManager notificationManager = 
    	  (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    	notificationManager.notify(NOTIFICATION_ID, n); */
    }
}